<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>闭包案例</title>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
</body>
<script>
    var nodes = document.getElementsByTagName('div');
    for(var i = 0,len = nodes.length;i<len;i++){
       
            nodes[i].onclick = function(){
                alert(i)//全是4
            }
       
       
    }
    for(let i = 0,len = nodes.length;i<len;i++){
       
       nodes[i].onclick = function(){
           alert(i)//0 1 2 3 4
        }
    
    
    }
    for(var i = 0,len = nodes.length;i<len;i++){
        (function(i){
            nodes[i].onclick = function(){
                alert(i)//0 1 2 3 4
            }
        })(i)
       
    }
</script>
</html>